<?php

class User_m extends Model {

    function User_m() {
        parent::Model();
        $this->load->database();
    }

    function loginInfor($username, $password) {
        $this->db->select('id,username, password');
        $this->db->from('lehu_users');
        $this->db->where(array('username' => $username, 'password' => $password));
        $query = $this->db->get();
        return $query;
    }

    function get_user_login_id($username) {
        $this->db->select('id, username');
        $this->db->from('lehu_users');
        $this->db->where('username', $username);
        $query = $this->db->get();
        return $query;
    }

    function update_user_last_login($username, $data) {
        $this->db->where('username', $username);
        $this->db->update('lehu_users', array('last_login' => $data));
    }

    function get_avatar_user($account_id) {
        if ($account_id > 0) {
            $this->db->select('lehu_profiles.gravatar');
            $this->db->from('lehu_profiles');
            $this->db->where('lehu_profiles.user_id', $account_id);
            $query = $this->db->get();
            return $query;
        }
        return array();
    }

    function register_user($data) {
        $query = $this->db->insert('lehu_users', $data);
        return $query;
    }

    function register_user_profiles($data) {
        $query = $this->db->insert('lehu_profiles', $data);
        return $query;
    }

    function get_user_register_id($email) {
        $this->db->select('id');
        $this->db->from('lehu_users');
        $this->db->where('email', $email);
        $this->db->order_by('id','DESC');
        $this->db->limit(1);
        $query = $this->db->get();
        return $query;
    }

    function check_register_exist_fields($field, $input_field) {
        $this->db->select($field);
        $this->db->from('lehu_users');
        $this->db->where($field, $input_field);
        $query = $this->db->get();
        return $query;
    }

    function change_password($id, $data) {
        $query = $this->db->update('lehu_users', $data, array('id' => $id));
        return $query;
    }

    function get_current_password($id, $password) {
        $this->db->select('lehu_users.password');
        $this->db->from('lehu_users');
        $this->db->where('id', $id);
        $this->db->where('password', $password);
        $query = $this->db->get();
        return $query;
    }

    function change_avatar($user_id, $data) {
        if ($user_id > 0) {
            $query = $this->db->update('lehu_profiles', $data, array('user_id' => $user_id));
            return $query;
        } else {
            return array();
        }
    }

    function get_deactivate_user($email) {
        $this->db->select();
        $this->db->from('lehu_users');
        $this->db->where(array('email' => $email));
        $this->db->where(array('active' => 0));
        $query = $this->db->get();
        return $query;
    }

    function get_user($email, $password) {
        $this->db->select('lehu_users.id,lehu_users.email,lehu_users.password,lehu_users.group_id,lehu_profiles.display_name,lehu_profiles.gravatar');
        $this->db->from('lehu_users');
        $this->db->join('lehu_profiles', 'lehu_profiles.user_id = lehu_users.id');
        $this->db->where(array('lehu_users.email' => $email, 'lehu_users.password' => $password));
        $query = $this->db->get();
        return $query;
    }

    function change_login_time($data, $userid) {
        $this->db->update('lehu_users', $data, array('id' => $userid));
    }

}